<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>发送邮件</title>
<link rel="stylesheet" href="${ctx}/statics/css/bootstrap.min.css" />
<link rel="stylesheet" href="${ctx}/statics/css/font-awesome.min.css" />
<link rel="stylesheet"
	href="${ctx}/statics/js/layer/skin/default/layer.css" />
<link rel="stylesheet"
	href="${ctx}/statics/js/bootstrap-table/bootstrap-table.css" />
<link rel="stylesheet" href="${ctx}/statics/js/toastr/toastr.css" />
</head>
<body>
	<div id="main-content">
		<div class="row">
			<div class="col-md-12">
				<div class="panel panel-default">
					<div class="panel-heading">发送邮件</div>
					<!-- /.panel-heading -->
					<div class="panel-body">
						<!-- 搜索                                                                -->
						<div class="row">
							<div class="col-xs-12">
								<!-- PAGE CONTENT BEGINS -->
								<form class="form-horizontal" role="form" id="entity_form">
									<input type="text" name="id" hidden="true">
									<div class="form-group">
										<label class="control-label col-sm-3">收件人：</label>
										<div class="col-sm-8">
											<select class="form-control" id="cu" name="customerId">
											</select>
										</div>
									</div>
									<div class="form-group">
										<label class="control-label col-sm-3">主题：</label>
										<div class="col-sm-8">
											<input type="text" class="form-control col-sm-3"
												name="emailTheme">
										</div>
									</div>
									<div class="form-group">
										<label class="control-label col-sm-3">内容：</label>
										<div class="col-sm-8">
											<!-- <input type="text" class="form-control col-sm-3"
								name="customerRemark"> -->
											<textarea class="form-control col-sm-3" rows="3" cols="1"
												name="emailContent"></textarea>
										</div>
									</div>
									<div class="form-group" hidden="true">
										<label class="control-label col-sm-3">保存方式</label>
										<div class="col-sm-8">
											<input type="text" class="form-control col-sm-3"
												name="emailState">
										</div>
									</div>
									<div class="form-group">
										<div align="center">
											<button class=" btn btn-sm btn-info"
												 onclick="sendEmail()">发送
											</button>
											<button class=" btn btn-sm btn-info" onclick="saveEmail()">
												存草稿</button>
										</div>
									</div>
								</form>
							</div>
						</div>
					</div>
					<!-- panel body -->
				</div>
			</div>
		</div>
	</div>
	<%@ include file="../common.jsp"%>
	<script type="text/javascript"
		src="${ctx}/statics/js/jquery-form/jquery.form.js"></script>
	<!-- 
		/* $.ajax({
			url : "${ctx}/emailInfo/sendEmail.do",
			type : "POST",
			data : $("#entity_form").serialize(),
			success : function(data) {
				if (data.success == true) {
					alert("success 发送邮件");
					window.location.href = "${ctx}/emailInfo/outboxListUI.do";
				} else {
					alert(data.errorMessage);
				}
			}
		}); */ -->
</body>
<script type="text/javascript">
	$(function intData() {
		$("#entity_form")[0].reset();
		var id = "${emailInfoVo.id}";
		var customerId = "${emailInfoVo.customerId}";
		var customerName = "${emailInfoVo.customerName}";
		var emailTheme = "${emailInfoVo.emailTheme}";
		var emailContent = "${emailInfoVo.emailContent}";
		if (customerId != null && customerId != '') {
			var cu = "<option value='"+customerId+"'>" + customerName
					+ "</option>";
			$("#cu").html(cu);
			$("input[name=id]").val(id);
			$("input[name=emailTheme]").val(emailTheme);
			$("textarea").val(emailContent);
		} else {
			checkExistCustomers();
		}
	});

	function sendEmail() {
		$("input[name=emailState]").val("1");
		var result = checkForm();
		if (!result) {
			return false;
		}
		$("#entity_form").attr("action", "${ctx}/emailInfo/sendEmail.do");
		$("#entity_form").submit();

	}
	function saveEmail() {
		$("input[name=emailState]").val("0");
		var result = checkForm();
		if (!result) {
			return false;
		}
		$("#entity_form").attr("action", "${ctx}/emailInfo/saveEmail.do");
		$("#entity_form").submit();
	}
	function checkForm() {
		var cu = $("#cu").val();
		if (cu==null||cu=='') {
			//toastr.warning("请填用户名称");
			alert("请选择客户发送邮件");
			return false;
		}
		return true;
	}
	//检查是否有分配的客户
	function checkExistCustomers() {
		$.ajax({
			url : "${ctx}/customer/getDataByCurrentUser.do",
			type : "POST",
			data : {},
			success : function(data) {
				if (data.success) {
					var html = "";
					if (data.result.length == 0) {
						var html = html
								+ "<option value=''>无分配的客户需要发送邮件</option>";
						$("#cu").html(html);
					} else {
						for (var i = 0; i < data.result.length; i++) {
							html = html
									+ "<option value='"+data.result[i].id+"'>"
									+ data.result[i].customerName + "</option>"
						}
						$("#cu").html(html);
					}
				} else {
					alert(data.errorMessage);
				}
			}
		});
	}
</script>
</html>